
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'admin_log2/index' + location.search,
                    add_url: 'admin_log2/add',
                    edit_url: '',  //方式二 ：隐藏修改和删除按钮
                    del_url: '',  //方式二 ：隐藏修改和删除按钮
                    multi_url: 'admin_log2/multi',
                    import_url: 'admin_log2/import',
                    table: 'admin_log',
                }
            });

            var table = $("#table");

            //窗口最大化
            table.on('load-success.bs.table', function (data) {
               $('.max').data('area', ['100%', '100%']);
            });

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
               var form = $("form", table.$commonsearch);
               $("input[name='username']", form).addClass("selectpage").data("source", "auth/admin/index").data("primaryKey", "username").data("field", "username").data("orderBy", "id desc");
                $("input[name='title']", form).addClass("selectpage").data("source", "auth/adminlog/selectpage").data("primaryKey", "title").data("field", "title").data("orderBy", "id desc");
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: __('Id'),
                            operate: false,
                            sortable: true,
                        },
                        // {field: 'admin_id', title: __('Admin_id')},
                        {
                            field: 'username',
                            title: __('Username'),
                            // operate: 'LIKE'
                        },
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE %...%',
                            placeholder:'模糊搜索 * 表示任意字符',
                            width: '280px',
                        },
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},

                        {
                            field: 'ip',
                            title: __('Ip'),
                            //operate: 'LIKE',
                            formatter: Controller.api.formatter.ip,
                            // events: Controller.api.events.ip,
                        },
                        {
                            field: 'admin_id',
                            title: __('联动搜索'),
                            searchList: function (column) {
                                return Template('categorytpl', {});
                            },
                        },
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('切换'),
                            operate: false,
                            formatter: Controller.api.formatter.custom,
                        },

                        // {field: 'useragent', title: __('Useragent'), operate: 'LIKE'},
                        //browser是一个不存在的字段
                        //通过formatter来渲染数据,同时为它添加上事件
                        {
                            field: 'browser',
                            title: __('Browser'),
                            formatter: Controller.api.formatter.browser,
                            events: Controller.api.events.browser,
                            operate: false,
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addClass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            width: "150px",
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            // 方式一：隐藏修改和删除按钮
                            // formatter: function (value, row, index) {
                            //     var that = $.extend({}, this);
                            //     $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                            //     $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                            //     that.table = table;
                            //     return Table.api.formatter.operate.call(that, value, row, index);
                            // },
                            buttons: [
                                {
                                    name: 'click',
                                    title: '点击执行事件',
                                    classname:　'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-leaf',
                                    click: function (data) {
                                        Layer.alert("点击按钮执行的事件"+JSON.stringify(data));
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: '弹出窗口打开',
                                    classname:　'btn btn-xs btn-primary btn-dialog max',
                                    icon: 'fa fa-list',
                                    url: 'admin_log2/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到的回传数据是：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'ajax',
                                    title: '发送Ajax',
                                    classname:　'btn btn-xs btn-success btn-ajax btn-magic',
                                    icon: 'fa fa-magic',
                                    confirm: '你是否要发送ajax请求',
                                    url: 'admin_log2/detail',
                                    success: function (data,ret) {
                                        Layer.alert(ret.msg + ", 返回数据是："+ JSON.stringify(data));
                                    },
                                    error: function (data,ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    title: '新选项卡中打开',
                                    classname:　'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'admin_log2/detail',
                                }
                            ]
                        }
                    ]
                ],
                searchFormVisible:true,
                onLoadSuccess:function(){
                    Backend.api.sidebar({
                        'admin_log2': 100
                    })
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail:function () {
            $(document).on('click', '.btn-callback', function () {
                Fast.api.close($("input[name=callback]").val())
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                browser: function (value, row, index) {
                    return '<a class="btn btn-xs btn-browser">' + row.useragent.split(" ")[0] + '</a>';
                },

                ip: function (value, row, index) {
                    //return '<a class="btn btn-xs bg-success btn-ip"><i class="fa fa-map-marker"></i> '+ value +'</a>'

                    url = "admin_log2?" +this.field+ "="+value ;
                    //return '<a href="'+url+'" class="label label-success addtabsit" title="'+__('搜索 %s', value)+'">'+ __('搜索 %s', value) +'</a>';
                    this.url = url;
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                },



                custom: function (value, row, index) {
                    //添加切换按钮
                    return '<a class="btn-change" ' +
                        'data-url="admin_log2/change" ' +
                        'data-confirm="确认切换状态？" ' +
                        'data-id="'+ row.id +'">' +
                        '<i class="fa '+ (row.title == '' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') +' fa-2x"></i> ' +
                        '</a>'
                },
            },
            events:{
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为：<code>" + JSON.stringify(row) + "</code>");
                    }
                },
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        // step1:赋值搜索
                        $("form.form-commonsearch [name='ip']").val(value);
                        // step2:进行点击
                        $("form.form-commonsearch").trigger('submit');
                        // step3:弹出toast
                        Toastr.info('执行了自定义搜索操作，value值是：'+value);
                    }
                }
            }
        }
    };
    return Controller;
});